﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps.Admin
{
    public partial class AddForumGroup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void btncreategroup_Click(object sender, EventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                ForumGroup forumgroup = new ForumGroup();
                forumgroup.datecreated = DateTime.Now;
                forumgroup.forumgroupname = txtforumgroupname.Text.Trim();
                db_connect.ForumGroups.Add(forumgroup);
                db_connect.SaveChanges();
                BindData();

            }
        }

        private void BindData()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forumgroup = db_connect.ForumGroups.OrderByDescending(f => f.forumgroupidPK).ToList();
                dgforumgrouplist.DataSource = forumgroup;
                dgforumgrouplist.DataBind();
            }
        }

        protected void dgforumgrouplist_EditCommand(object sender, DataGridCommandEventArgs e)
        {
            dgforumgrouplist.EditItemIndex = e.Item.ItemIndex;
            BindData();
        }

        protected void dgforumgrouplist_Cancel(object sender, DataGridCommandEventArgs e)
        {
            dgforumgrouplist.EditItemIndex = -1;
            BindData();
        }

        protected void dgforumgrouplist_Update(object sender, DataGridCommandEventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                TextBox forumgrouptitle = (TextBox)dgforumgrouplist.Items[e.Item.ItemIndex].FindControl("txtforumgroupname");

                Label forumgroupidPK = (Label)dgforumgrouplist.Items[e.Item.ItemIndex].FindControl("lbforumgroupid");
                int forumid = int.Parse(forumgroupidPK.Text);
                var forumgroup = db_connect.ForumGroups.Where(f => f.forumgroupidPK.Equals(forumid)).SingleOrDefault();
                forumgroup.forumgroupname = forumgrouptitle.Text.Trim();
                db_connect.SaveChanges();
                dgforumgrouplist.EditItemIndex = -1;
                BindData();
            }
        }

        protected void dgforumgrouplist_Paging(object sender, DataGridPageChangedEventArgs e)
        {
            dgforumgrouplist.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }
    }
}